<?php
// +----------------------------------------------------------------------
// | 选座 模型
// +----------------------------------------------------------------------
namespace app\wechat\model;

use think\Model;
use think\Db;

class Reservation extends Model{
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = '';
    protected $autoWriteTimestamp = true;
    /**
     * 获取分区某时间段已选座位数组
     * @param int $stime 开始时间戳
     * @param int $etime 结束时间戳
     * @param int $areaid 区域id
     * @return string
     */
    public function getUseCount($stime,$etime,$areaid){
        $table = config('database.prefix').'reservation';
        $res = Db::query("SELECT seatid FROM $table WHERE areaid=$areaid AND status<>1 And ((startTime BETWEEN $stime AND $etime) OR (endTime BETWEEN $stime AND $etime AND (leaveTime = 0 OR leaveTime > $stime)) OR (startTime < $stime AND endTime >$etime AND (leaveTime = 0 OR leaveTime > $stime)))");
        return $res;
    }
    /**
     * 获取当天预约次数
     * @param int $userid 用户id
     * @return string
     */
    public function getTodayCount($userid){
        $total = self::where('userid',$userid)->whereTime('ctime', 'd')->count();
        return $total;
    }
    /**
     * 该座位是否已经预约
     * @param int $seatid 座位id
     * @return string
     */
    public function isReservation($seatid,$stime,$etime){
        $reservation = new Reservation;
        $res = $reservation->where('seatid','exp'," = $seatid AND status<>1 AND ((starttime <= $stime AND endtime >=$stime) OR ( starttime <= $etime AND endtime >= $etime) OR (starttime > $stime AND endtime <$etime))")->select();
        return $res ? true:false;
    }
    /**
     * 该用户在该时间段是否已经预约
     * @param int $seatid 座位id
     * @return string
     */
    public function isUserTime($userid,$stime,$etime){
        $reservation = new Reservation;
        $res = $reservation->where('userid','exp'," = $userid AND status<>1 AND ((starttime <= $stime AND endtime >=$stime) OR ( starttime <= $etime AND endtime >= $etime) OR (starttime > $stime AND endtime <$etime))")->select();

        return $res ? true:false;
    }
    /**
     * 添加修改单条数据
     * @param array $data 修改、添加数据
     * @param array $map 更新条件
     * @return bool
     */
    public function updateData($data,$map=[]){
        if (empty($map)) {
            $res = $this->save($data);
        }else{
            $res = $this->save($data,$map);
        }
        return $res ? true:false;
    }
    /**
     * 添加修改多条数据
     * @param array $data 修改、添加数据
     * @return bool
     */
    public function updateAllData($data){
        $res = $this->saveAll($data);
        return $res ? true:false;
    }
    /**
     * 获取预约座位信息
     * @param array $data 查询条件
     * @return bool
     */
    public function getOne($data){
        $res = self::get($data);
        return $res ? $res : false;
    } 
    /**
     * 获取用户所有预约
     * @param array $data 查询条件
     * @param int $page 当前页码
     * @return obj
     */
    public function getAll($data,$page){
        $limit = 10;
        $res = self::where($data)->field('id,userid,areaid,seatNum,seatType,startTime,endTime,status')->order('id','desc')->paginate($limit,false,['page'=>$page])->toArray();
        return $res ? $res : false ;
    }
    /**
     * 获取所有预约需要发送信息的数据
     * @param array $data 查询条件
     * @param int $page 当前页码
     * @return obj
     */
    public function getTimeAll($data){
        $res = self::where($data)
            ->alias('r')
            ->join('user u','r.userid = u.id')
            ->field('r.id,openid,r.status,areaid,seatNum,startTime,endTime,formid')->select();
        return $res ? $res : false ;
    }
    /**
     * 获取开启签退违规的数据
     * @param array $qddata 签到查询条件
     * @param array $qtdata 签退查询条件
     * @param int $page 当前页码
     * @return obj
     */
    public function getViolationAll($qddata,$qtdata){
        $res = self::where(function ($query) use($qddata) {
            $query->where($qddata);
        })->whereOr(function ($query) use ($qtdata) {
            $query->where($qtdata);
        })->field('id,userid,status')->select();
        return $res ? $res : false ;
    }

}